package com.logicwin.RobotTravel.Screens;


//import com.logicwin.RobotTravel.Services.Impl.PositionService;
import com.logicwin.RobotTravel.Services.Impl.ServiceManager;

import com.logicwin.Views.NavigateView;
import android.content.Context;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.RelativeLayout;

import com.logicwin.RobotTravel.Navigate.ScreenNavigateImpl;
import com.logicwin.RobotTravel.Navigate.messages.BaseEvent;
import com.logicwin.RobotTravel.Navigate.messages.MessageNavigateBase;

/*import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;*/

import com.logicwin.RobotTravel.CueDialog;
import com.logicwin.RobotTravel.IntroDialog;
import com.logicwin.RobotTravel.CueDialogInterface;
import com.logicwin.RobotTravel.Main;
import com.logicwin.RobotTravel.R;
public class ScreenNavigate extends Screen implements Runnable {
	private static Context mContext;
	ScreenNavigateImpl mImpl;
	private NavigateView mView;
	private Thread mThread;
	private boolean mRunning = true;
	private final static String TAG = ScreenNavigate.class.getCanonicalName();
	CueDialog mCueDialog = null;
	private Handler mHandler = new Handler(){
		public void handleMessage(Message msg){
/*			Bundle bundle = msg.getData();
			bundle.getString(key)*/
			Bundle bundle = msg.getData();
			final String viewname = bundle.getString("viewname");
			final String filename = bundle.getString("audioname");
			final int audioId = bundle.getInt("audioid");
			Log.i(TAG, filename);
			switch(msg.what){
/*			case 1:
				Log.i(TAG, "Handler::handleMessage(): ");
				if(ServiceManager.getPositoinService().getCurPosition() == null) {
					mImpl.playMusic(nullSound);
					return;
				}
				mImpl.playMusic(ServiceManager.getPositoinService().getCurPosition().getAudio());
				break;*/			
			case MessageNavigateBase.NO_WIFI:
/*				mImpl.playMusic(nullSound);*/
				Log.i(TAG, "NO_WIFI");
				break;				
			case MessageNavigateBase.WIFI_CHANGE:
				Log.i(TAG, "WIFI_CHANGE");
				if(mCueDialog == null){
					mCueDialog = new CueDialog(ScreenNavigate.mContext, viewname,new CueDialogInterface(){
						public void OnClick(){
							mImpl.playMusic(audioId);
							mCueDialog = null;
							
						}},
						new CueDialogInterface(){
						public void OnClick(){
							mCueDialog = null;
						}});   
					mCueDialog.setDisplay();	
					
				}
				break;
			case MessageNavigateBase.WIFI_NOCHANGE:
				Log.i(TAG, "WIFI_NOCHANGE");
				break;

			}
			super.handleMessage(msg);   
		}
	};
	
	public ScreenNavigate() {
		super(SCREEN_TYPE.NAVIGATE_T, TAG);
		mContext = this;
		mImpl = new ScreenNavigateImpl(this);
	}	
	public Handler getHandler(){
		return mHandler;
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	
        setContentView(R.layout.screen_navigate);  
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.map); 
        int width = bitmap.getWidth();  
        int height = bitmap.getHeight(); 
        int newWidth = Main.screenWidth;  
        int newHeight = Main.screenHeight;  
        float scaleWidth = ((float) newWidth) / width;  
        float scaleHeight = ((float) newHeight) / height;          
        
        Matrix matrix = new Matrix();  
        matrix.postScale(scaleWidth, scaleHeight);
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width,  
        		height, matrix, true);  
        Log.i(TAG, "width is "+ resizedBitmap.getWidth()+",height is "+resizedBitmap.getHeight());
        BitmapDrawable bmd = new BitmapDrawable(resizedBitmap); 
        RelativeLayout layout = (RelativeLayout) this.findViewById(R.id.navigate_relativeLayout);
        layout.setBackgroundDrawable(bmd);        


        mView = (NavigateView)this.findViewById(R.id.NavigateView);        
        		
	}	
			
    public void run() {  
    	int nInternal = 0;
        while (mRunning) {  
            try {  
            	Thread.sleep(200); 
            	if(mImpl.getActive())
            	{
	            	mView.postInvalidate();
	            	if((nInternal++)%10 == 0)
	            		mImpl.ScanWifi();
	            	for(int i=0; i<mImpl.getEventQueueSize(); i++){
	            		BaseEvent event = mImpl.getEvent();
	            		event.onFire();
	            	}
            	}
            } catch (InterruptedException e) {  
                Thread.currentThread().interrupt();  
            }    
        }  
    }  

	protected void onResume()
	{	
//		mThread.resume();
		mImpl.setActive(true);
		Log.v(TAG, "onResume()");
		super.onResume();
	}

	protected void onStart()
	{
		mImpl.setupWifi();
		mThread = new Thread(this); 
		mRunning = true;
		mThread.start();
		Log.v(TAG, "onStart()");
		super.onStart();
	}	
	@Override
	public void onStop() {
		mImpl.unRegWifi();
		mRunning = false;
		Log.v(TAG, "onStop()");
		super.onStop();
	}	
	@Override
	protected void onPause() {
//		mThread.suspend();
		mImpl.clearPositionInfo();
		mImpl.setActive(false);
		mImpl.stopPlayMusic();
		// TODO Auto-generated method stub
		Log.v(TAG, "onPause()");
		super.onPause();
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		Log.v(TAG, "onDestroy()");
		super.onDestroy();
	}	
    public boolean onTouchEvent(MotionEvent event)
    {        
        switch (event.getAction())
        {
            case MotionEvent.ACTION_DOWN:
            	            	
            	float xPos = event.getX();
            	float yPos = event.getY();
            	if(ServiceManager.getPositoinService().hitUndo((int)xPos, (int)yPos)){            		
            		onPause();
            		ServiceManager.getScreenService().back();
            	}             	
            	xPos = xPos*800/Main.screenWidth;
            	yPos = yPos*480/Main.screenHeight;           	
            	Log.i(TAG,"MobileMenuActivity::onTouchEvent is catched. x y is"+xPos + ","+yPos);
            	if( ServiceManager.getPositoinService().hitCurrPosition((int)xPos, (int)yPos)) {          			

            		Intent intent = new Intent(this, IntroDialog.class);
            		intent.putExtra("sceen", "sceen" + ServiceManager.getPositoinService().getCurPositionId());
                    this.startActivity(intent);
	            	return true;
            	}

/*            	if( ServiceManager.getPositoinService().hitControl((int)xPos, (int)yPos)){
            		if(ServiceManager.getPositoinService().getPlay() == false) {
            			ServiceManager.getPositoinService().setPlay(true);
            			
            		}
            		else	{
            			if(mMP.isPlaying()) mMP.stop();
            			ServiceManager.getPositoinService().setPlay(false);
            		}
            	}*/
            	return false; 
            case MotionEvent.ACTION_UP:
            	
                return true;

        }
        return super.onTouchEvent(event);
    }		
}
